/*
 * Copyright (c) 2016. Naivor.All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.myapplication.base.mvvm;


import com.example.myapplication.base.ApiException;
import com.harmonyos.livedata.MutableLiveData;
import com.harmonyos.viewmodel.HarmonyViewModel;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import ohos.app.Context;

import java.util.List;

public abstract class BaseViewModel extends HarmonyViewModel {
    private CompositeDisposable compositeDisposable;

    private UIChangeLiveData uiChangeLiveData;

    public BaseViewModel(Context application) {
        super(application);
        compositeDisposable = new CompositeDisposable();
        uiChangeLiveData = new UIChangeLiveData();
    }


    public void addTask(Disposable disposable) {
        compositeDisposable.add(disposable);
    }

    public void removeTask(Disposable disposable) {
        compositeDisposable.remove(disposable);
    }

    @Override
    public void onCleared() {
        clearTask();
        super.onCleared();
    }

    public void clearTask() {
        compositeDisposable.clear();
    }

    private int page = 0;

    protected boolean isNeedShowErrorView = false;

    public UIChangeLiveData getUIChangeLiveDataEvent() {
        return uiChangeLiveData;
    }

    public void loadComplete() {
        postDismissLoadingDialogEvent(true);
    }

    public void loadError(Throwable e) {
        String errorMsg = "";
        if (e instanceof ApiException) {
            errorMsg = ((ApiException) e).getMsg();
        } else {
            errorMsg = "网络异常";
        }
//        Log.d(TAG, "loadError: ---------$e")
        if (!(e instanceof ApiException) && isNeedShowErrorView) {
            postShowNetworkErrorEvent(true);
        }
        postShowToastMessageEvent(errorMsg);
        postDismissLoadingDialogEvent(true);

    }

    public void loadListData(boolean isRefresh) {
        if (!isRefresh) {
            postShowLoadingNowEvent(false);
        }
        isNeedShowErrorView = isRefresh;
        if (isRefresh) {
            refreshPageData();
        }
    }

    public int loadMore() {
        page++;
        return page;
    }

    /**
     * 刷新页面数据
     */
    public void refreshPageData() {
        page = 0;
    }

    public boolean isLoadMore() {
        return page > 1;
    }


    public void setListData(List listData, MutableLiveData<List> listDataEvent) {
        postShowNetworkErrorEvent(false);
        if (listData.isEmpty()) {
            postShowEmptyViewEvent(true);
        } else {
            postShowEmptyViewEvent(false);
            listDataEvent.setValue(listData);
        }
    }

    public class UIChangeLiveData {
        private MutableLiveData<Boolean> showLoadingNowEvent ;

        private MutableLiveData<Boolean> dismissLoadingDialogEvent;

        private MutableLiveData showToastMessageEvent;

        private MutableLiveData<Boolean> showEmptyViewEvent ;

        private MutableLiveData<Boolean> showNetworkErrorEvent;

        public MutableLiveData<Boolean> getShowLoadingNowEvent() {
            if(showLoadingNowEvent==null)showLoadingNowEvent= new MutableLiveData();
            return showLoadingNowEvent;
        }

        public MutableLiveData<Boolean> getDismissLoadingDialogEvent() {
            if(dismissLoadingDialogEvent==null)dismissLoadingDialogEvent= new MutableLiveData();
            return dismissLoadingDialogEvent;
        }

        public MutableLiveData<String> getShowToastMessageEvent() {
            if(showToastMessageEvent==null)showToastMessageEvent= new MutableLiveData();
            return showToastMessageEvent;
        }

        public MutableLiveData<Boolean> getShowEmptyViewEvent() {
            if(showEmptyViewEvent==null)showEmptyViewEvent= new MutableLiveData();
            return showEmptyViewEvent;
        }

        public MutableLiveData<Boolean> getShowNetworkErrorEvent() {
            if(showNetworkErrorEvent==null)showNetworkErrorEvent= new MutableLiveData();
            return showNetworkErrorEvent;
        }
    }


    public void postShowLoadingNowEvent(boolean isNow) {
        uiChangeLiveData.showLoadingNowEvent.postValue(isNow);
    }

    public void postDismissLoadingDialogEvent(boolean dismiss) {
        uiChangeLiveData.dismissLoadingDialogEvent.postValue(dismiss);
    }

    public void postShowEmptyViewEvent(boolean show) {
        uiChangeLiveData.showEmptyViewEvent.postValue(show);
    }

    public void postShowToastMessageEvent(String message) {
        uiChangeLiveData.showToastMessageEvent.postValue(message);
    }

    public void postShowNetworkErrorEvent(boolean show) {
        uiChangeLiveData.showNetworkErrorEvent.postValue(show);
    }


}